<?php

require_once dirname(__FILE__).'/../lib/doctor_question_and_answerGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/doctor_question_and_answerGeneratorHelper.class.php';

/**
 * doctor_question_and_answer actions.
 *
 * @package    docplus.co.za
 * @subpackage doctor_question_and_answer
 * @author     Fox Matuku
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class doctor_question_and_answerActions extends autoDoctor_question_and_answerActions
{
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request)
    {
        $this->doctor = DoctorTable::getInstance()->findOneByUserId($request->getParameter("doctor_id"));
    }

    /**
     * Executes askDone action
     *
     * @param sfRequest $request A request object
     */
    public function executeAskDone(sfWebRequest $request)
    {
        $this->doctorQuestionAndAnswers = DoctorQuestionAndAnswerTable::getInstance()->retrieveByDoctorId($request->getParameter("doctor_id"));
    }

    /**
     * Executes answerDone action
     *
     * @param sfRequest $request A request object
     */
    public function executeAnswerDone(sfWebRequest $request)
    {
        $this->doctorQuestionAndAnswers = DoctorQuestionAndAnswerTable::getInstance()->retrieveByDoctorId($request->getParameter("doctor_id"));
    }

    /**
     * Executes ask action
     *
     * @param sfRequest $request A request object
     */
    public function executeAsk(sfWebRequest $request)
    {
        $this->doctorId = $request->getParameter('doctor_id', null);
        $this->forward404Unless($this->doctorId);

        $doctorQuestionAndAnswer = new DoctorQuestionAndAnswer();
        $doctorQuestionAndAnswer->setDoctorId($this->doctorId);
        
        $this->form = new DoctorQuestionForm($doctorQuestionAndAnswer);
        if ($request->isMethod('post'))
        {
            $this->form->bind($request->getParameter($this->form->getName()));
            if ($this->form->isValid())
            {
                try
                {
                    $doctorQuestionAndAnswerId = $this->form->save()->getId();

                    $this->getUser()->setFlash('notice', 'Thanks for successfully submitting your question!');

                    echo $doctorQuestionAndAnswerId;
                    exit;
                }
                catch (Exception $e)
                {
                    
                }
            }
        }
    }

    /**
     * Executes answer action
     *
     * @param sfRequest $request A request object
     */
    public function executeAnswer(sfWebRequest $request)
    {
        $doctorQuestionAndAnswerId = $request->getParameter('doctor_question_and_answer_id', null);
        $this->forward404Unless($doctorQuestionAndAnswerId);
        
        $doctorQuestionAndAnswer = DoctorQuestionAndAnswerTable::getInstance()->findOneById($doctorQuestionAndAnswerId);
        $this->forward404Unless($doctorQuestionAndAnswerId);
        
        $this->form = new DoctorAnswerForm($doctorQuestionAndAnswer);
        if ($request->isMethod('post'))
        {
            $this->form->bind($request->getParameter($this->form->getName()));
            if ($this->form->isValid())
            {
                try
                {
                    $doctorQuestionAndAnswerId = $this->form->save()->getId();

                    $this->getUser()->setFlash('notice', 'Thanks for successfully submitting your answer.');

                    echo $doctorQuestionAndAnswerId;
                    exit;
                }
                catch (Exception $e)
                {                    
                }
            }
        }
    }
}
